<template>
  <el-dialog
    title="提示"
    :visible.sync="dialogVisible"
    width="30%"
    @close="close"
    @open="dialogOpen"
  >
    <el-form ref="addRef" :model="addForm" :rules="addRules" label-width="100px">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="addForm.username" style="width:80%" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="addForm.mobile" style="width:80%" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="addForm.timeOfEntry" style="width:80%" placeholder="请选择入职时间" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="addForm.formOfEmployment" style="width:80%" placeholder="请选择">
          <el-option
            v-for="item in formType"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="addForm.workNumber" style="width:80%" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <!-- <el-input v-model="addForm.departmentName" style="width:80%" placeholder="请选择部门"> -->
        <!-- show-all-levels="false" 取最后的一个对象 -->
        <el-cascader
          v-model="addForm.departmentName"
          :options="options"
          :props="defaultProps"
          :show-all-levels="false"
        />
        <!-- </el-input> -->
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker v-model="addForm.correctionTime" style="width:80%" placeholder="请选择转正时间" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </span>
  </el-dialog>

</template>

<script>
import { getDepartmentList, addEmployee } from '@/api'
export default {
  name: 'AddEmployee',
  props: {
    dialogVisible: {
      type: Boolean
    }
  },
  data() {
    return {
      addForm: {
        username: '', // 用户名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      },
      formType: [
        {
          value: 1,
          label: '正式'
        },
        { value: 2,
          label: '非正式'
        }
      ],
      options: [],
      defaultProps: {
        value: 'name',
        label: 'name',
        checkStrictly: true
      },
      addRules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: ['blur', 'change'] },
          { min: 2, max: 4, message: '用户姓名为2-4位', trigger: ['blur', 'change'] }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: ['blur', 'change'] },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: ['blur', 'change'] }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: ['blur', 'change'] }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: ['blur', 'change'] }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: ['blur', 'change'] }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  methods: {
    // 新增员工提交
    async submit() {
      const vaild = await this.$refs.addRef.validate()
      if (vaild) {
        // 提交接口
        await addEmployee({ ...this.addForm, departmentName: this.addForm.departmentName[this.addForm.departmentName.length - 1] })
        // 提示用户
        this.$message.success('新增员工成功')
        // 关闭弹窗
        this.close()
        // 重新渲染
        this.$emit('update')
      }
    },
    close() {
      this.$emit('close')
      // form 表单的重置
      this.$refs.addRef.resetFields()
      // 手动重置
      this.addForm = {

      }
    },
    async dialogOpen() {
      const res = await getDepartmentList()
      this.options = this.findChildren(res.data.depts, '')
    },
    findChildren(arr, id = '') {
      const children = []
      arr.forEach(item => {
        if (item.pid === id) {
          children.push(item)
          if (this.findChildren(arr, item.id).length > 0) {
            item.children = this.findChildren(arr, item.id)
          }
        }
      })
      return children
    }
  }
}
</script>

<style>

</style>
